package com.hyb.algorithm.data.struct.cipher.sm2;

import org.bouncycastle.asn1.*;
import org.bouncycastle.util.encoders.Base64;

import java.io.IOException;

/**
 * @author huyanbing
 * @create 2023/11/22 4:12 下午
 */
public class Sm2PrivKey {

    public static void main(String[] args) throws IOException {
        ASN1EncodableVector alg = new ASN1EncodableVector();
        alg.add(new ASN1ObjectIdentifier("1.2.840.10045.2.1"));
        alg.add(new ASN1ObjectIdentifier("1.2.156.10197.1.301"));
//这个是BEGIN EC PRIVATE KEY- 和 END EC PRIVATE KEY-中间的部分
        String key = "MHcCAQEEIK3k1lZP4KZ/EJaXy9ezX7fIK82PAy3apbgkB99NXhUroAoGCCqBHM9VAYItoUQDQgAEyFqMJbk2G4g0INjPaYks3A7UGFveMpdw6uH3vGoexijjAs35Iz4j+Gt214dXQUj8bS7OQtkABwCpjq38sG5jEw==";
        DEROctetString derOctetString = new DEROctetString(Base64.decode(key));

        ASN1EncodableVector v = new ASN1EncodableVector();
        v.add(new ASN1Integer(0));
        v.add(new DERSequence(alg));
        v.add(derOctetString);
        DERSequence sm2privatekey = new DERSequence(v);

        System.out.println(Base64.toBase64String(sm2privatekey.getEncoded()));
    }
}
